package com.hahaliu.hahaliu.controller.user;

import com.hahaliu.hahaliu.common.result.Result;
import com.hahaliu.hahaliu.common.utils.RedisUtil;
import com.hahaliu.hahaliu.common.context.CurrentUser;
import com.hahaliu.hahaliu.pojo.entity.Danmu;
import com.hahaliu.hahaliu.service.video.DanmuService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/user/danmu")
@Api(tags = "用户端弹幕相关接口")
@Slf4j
public class DanmuController {

	@Autowired
	private DanmuService danmuService;

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private CurrentUser currentUser;

	/**
	 * 获取弹幕列表
	 * @param vid   视频ID
	 * @return  CustomResponse对象
	 */
	@GetMapping("/list/{vid}")
	public Result<List<Danmu>> getDanmuList(@PathVariable("vid") String vid) {
		Set<Object> idset = redisUtil.getMembers("danmu_idset:" + vid);
		List<Danmu> danmuList = danmuService.getDanmuListByIdset(idset);
		return Result.success(danmuList);
	}

	/**
	 * 删除弹幕
	 * @param id  弹幕id
	 * @return  响应对象
	 */
	@PostMapping("/danmu/delete")
	public Result deleteDanmu(@RequestParam("id") Integer id) {
		Integer loginUid = currentUser.getUserId();
		danmuService.deleteDanmu(id, loginUid, currentUser.isAdmin());
		return Result.success();
	}
}
